<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="name"></el-input>
      <el-button @click="removelist">清除</el-button>
      <el-button type="primary" @click="searchList">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="full">
      <el-table-column type="index" label="任务编号" width="80" :index="(params._page -1) * params._limit +1"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{row}">
          <el-button size="mini" style="margin-right: 10px" @click="compile(row.id)">编辑</el-button>
          <el-popconfirm confirmButtonText="好的" cancelButtonText="不用了" icon="el-icon-info" iconColor="red" title="确定删除任务吗" @confirm=DeleteList(row.id)>
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination layout="prev, pager, next" :total="+ total" @current-change="pageChange" :page-size="params. _limit" :current-page="params._page"> </el-pagination>
      <el-button size="small" plain @click="NewlyLncreased">+ 新增</el-button>
    </div>
    <template>
      <el-dialog :title="this.roleForm.id ? '编辑' : '新增'" :visible="dialogVisible" width="30%" @close="BouncedOff">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="roleForm.des" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="BouncedOff">取 消</el-button>
          <el-button type="primary" @click="confirms">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import {
  QueryListAPI,
  AddedListAPI,
  DeleteListAPI,
  CompileListAPI,
  EditTheListAPI,
  SearchListingsAPI
} from '@/api/user.js'
import { v4 as uuidv4 } from 'uuid'

export default {
  data() {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      full: [], //
      dialogVisible: false,
      // 新增编辑的数据项
      roleForm: {
        name: '',
        des: '',
        id: ''
      }, //新增数据项
      name: '',
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur' }
        ],
        des: [
          { required: true, message: '请输入任务描述', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 请求数据接口
    async getList() {
      const res = await QueryListAPI(this.params)
      this.full = res.data
      this.total = res.headers['x-total-count']
    },
    // 请求的每页数据发给后台
    pageChange(page) {
      this.params._page = page
      this.getList()
    },
    // 弹框显示
    NewlyLncreased() {
      this.dialogVisible = true
    },
    // 弹框关闭点x号
    BouncedOff() {
      this.dialogVisible = false

      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        des: '',
        id: ''
      }
    },
    // 新增或编辑的地方
    confirms() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          if (this.roleForm.id) {
            await EditTheListAPI(this.roleForm)
            this.$message.success('编辑成功')
            // this.roleForm.id = ''
          } else {
            let uuid = uuidv4()
            this.roleForm.id = uuid
            this.params._page = Math.ceil(++this.total / this.params._limit)
            await AddedListAPI(this.roleForm)
            // 优化新增跳转到最后一页
            if (this.isLastPageFulled) {
              this.params._page = this.maxNum + 1
            } else {
              this.params._page = this.maxNum
            }
            this.$message.success('新增成功')
          }
          this.getList()
          this.BouncedOff()
        }
      })
    },
    // 删除
    async DeleteList(id) {
      await DeleteListAPI(id)
      this.$message.success('删除成功')
      // 删除当前没数据跳转到上页
      if(this.full.length === 1) {
          this.params._page--
          if (this.params._page <= 0) {
            this.params._page = 1
          }
        }
      this.getList()
    },
    // 编辑
    async compile(id) {
      this.dialogVisible = true
      const res = await CompileListAPI(id)
      this.roleForm = res.data
    },
    // 清空搜索内容
    removelist() {
      this.name = ''
      this.getList()
    },
    // 搜索
    async searchList() {
      const res = await SearchListingsAPI(this.name)
      this.full = res.data
    }
  },

  computed: {
    isLastPage() {
      const maxNum = Math.ceil(this.total / this.params._limit)
      return this.params._page === maxNum
    },
    maxNum() {
      return Math.ceil(this.total / this.params._limit)
    },
    isLastPageFulled() {
      return this.total % this.params._limit === 0
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
